package com.wang.config;

import com.wang.domain.*;
import com.wang.extension.TestBeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

/**
 * @Configuration代表这是一个Bean配置类，可以结合@Bean注解注入bean
 */
@Configuration
public class MyConfig {
    /**
     * 基础Bean注入
     */
    @Bean
    public BasicBean wangBean() {
        return new BasicBean();
    }

    /**
     * 多例Bean注入，默认Scope是singleton
     */
    @Bean
    @Scope(value = "prototype")
    public PrototypeBean myPrototypeBean() {
        return new PrototypeBean();
    }

    /**
     * 使用FactoryBean创建spring bean
     */
    @Bean
    public MyFactoryBean wlyFactoryBean() {
        return new MyFactoryBean();
    }

    @Bean
    public ParamBeanFactory wlyBeanFactory() {
        return new ParamBeanFactory();
    }

    /**
     * 创建Bean时，参数会先从spring容器中获取
     */
    @Bean
    public ParamBean wlyBean(ParamBeanFactory paramBeanFactory) {
        return paramBeanFactory.create();
    }

    //==========================测试Bean初始化和销毁执行函数=============================
    @Bean
    public InitDestory1 initDestory1() {
        return new InitDestory1();
    }

    @Bean(initMethod = "init", destroyMethod = "destory")
    public InitDestory2 initDestory2() {
        return new InitDestory2();
    }

    @Bean
    public InitDestory3 initDestory3() {
        return new InitDestory3();
    }

    @Bean(initMethod = "customInit", destroyMethod = "customDestory")
    public InitDestory4 initDestory4() {
        return new InitDestory4();
    }

    //==========================测试同类型注入多个Bean=============================
    @Bean
    public MultipartBean wlyMultipartBean1() {
        return new MultipartBean();
    }

    /**
     * @Primary代表同一个类型注入了多个Bean的时候，@Autowired引入这个类型的Bean且没使用Qualifier指定Bean的名称，@Primary的生效
     */
    @Bean
    @Primary
    public MultipartBean wlyMultipartBean2() {
        return new MultipartBean();
    }

    //=======================依赖注入测试=================
    @Bean
    public DependencyInjectBean wlyDIBean() {
        return new DependencyInjectBean();
    }

    /**
     * bean中使用applicationContext
     * 1.直接Autowired
     * 2.实现ApplicationContextAware接口
     * 3.构造函数传入，spring会先初始化构造函数中的bean
     */
    @Bean
    public ApplicationContextBean wlyApplicationContext() {
        return new ApplicationContextBean();
    }

    @Bean
    public ApplicationContextBean2 wlyApplicationContext2() {
        return new ApplicationContextBean2();
    }


    @Bean
    public User wlyUser(){
        return new User();
    }
}
